package com.my.study.algorithm.leetcode;

/**
 * 有序二维数组
 * 
 * @author Carlos
 * @version 1.0.0
 * @date 2023/2/25 20:29
 */
public class SearchMatrix {


    public static void main(String[] args) {
        int[][] m = new int[4][4];

        int v = 1;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                m[i][j] = v++;
            }
        }

        System.out.println(m);

        boolean b = searchMatrix(m, 17);
        System.out.println(b);
    }

    public static boolean searchMatrix(int[][] matrix, int target) {
        int row = matrix.length - 1;
        int column = matrix[0].length - 1;

        int i = 0;
        int j = column;
        
        while (i <= row && j >= 0) {
            int v = matrix[i][j];
            if (v == target) {
                return true;
            }
            if (v < target) {
                i++;
            }
            else {
                j--;
            }
        }
        
        return false;
    }
}
